package com.dchh.android.suverynew.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.adapter.TZPSListAdapter;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.bean.Task;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.FileUtil;
import com.test.liudi.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务包图纸拍摄
 *
 * @author zhoudan
 * @date 2016年2月22日
 */
public class PKGPhotoActivity extends SBaseActivity {

    private ListView listview;
    private List<Task> taskList = new ArrayList<Task>();
    private TZPSListAdapter adapter;
    private String root;
    private String taskName;
    private String dbPath;

    private DBManagerUtil dbManagerUtil;
    private String beizhu2 = "2图纸";
    private String dcqb;

    private int count = 0, num = 0, cun;
    private String period;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paper_photo_add_list);
    }

    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText("县列表");
    }

    @Override
    public void initView() {
        root = SPreferencesManager.getValue(MSApplication.TASK_SAVE_PATH,
                FileUtil.getSavePath());
        taskName = getIntent().getStringExtra("taskName");
        dbPath = root + "/" + taskName + "/TASK/TRANSPORT/DATA.db";

        period = getIntent().getStringExtra("period");
        period = period.contains("早稻面积") ? "春播面积" : period;
        dcqb = period + "-" + "遥感面积调查表";

        dbManagerUtil = DBManagerUtil.getInstance(dbPath, this);
        getDatas();
        listview = (ListView) findViewById(R.id.photo_listView);
        if (adapter == null) {
            adapter = new TZPSListAdapter(this, taskList);
            adapter.setCount(count);
            adapter.setOnOperateListener(listener);
            listview.setAdapter(adapter);
        }

        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Intent intent = new Intent(PKGPhotoActivity.this, CUNPhotoActivity.class);
                intent.putExtra("period", period);
                intent.putExtra("taskName", taskName);
                startActivity(intent);
            }
        });
    }

    TZPSListAdapter.OnOperateListener listener = new TZPSListAdapter.OnOperateListener() {

        @Override
        public void OnTake(int position) {
            //县级图纸拍摄不做限制
            /*if(num == 0 || cun > num){
                isAlertDialog(PKGPhotoActivity.this, "下级村图纸拍摄未完成，请先完成村级图纸拍摄！", null, "确定");
            }else{*/
            Intent intent = new Intent(PKGPhotoActivity.this, TZPhotoActivity.class);
            intent.putExtra("type", 1);
            intent.putExtra("period", period);
            intent.putExtra("taskName", taskName);
            startActivity(intent);
            /*}*/
        }

        @Override
        public void OnShow(int position) {

            Intent intent = new Intent(PKGPhotoActivity.this, TZPhotoActivity.class);
            intent.putExtra("type", 1);
            intent.putExtra("period", period);
            intent.putExtra("taskName", taskName);
            startActivity(intent);
        }

    };

    private void getDatas() {
        taskList.clear();
        taskList.add(Constants.currentTask);
        String wh = " TYPE = '1' ";
        count = dbManagerUtil.getTZPhotoCount("PHOTO", wh, dbPath);
        LogUtils.e(this, TAG, "县：" + count);
        String wh2 = " COUNTY = '" + taskName + "' and  DCQB = '" + dcqb + "' and TYPE = '2'";
        num = dbManagerUtil.getTZPhotoCount("PHOTO", wh2, dbPath);
        String cundbpath = root + "/" + taskName + "/TASK/DATA.db";
        String cuntable = Constants.nodelist.get(0).getTable();
        cun = dbManagerUtil.getTZPhotoCount(cuntable, null, cundbpath);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Constants.data_refresh) {
            Constants.data_refresh = false;
            count = dbManagerUtil.getTZPhotoCount("PHOTO", " TYPE = '1' ", dbPath);
            adapter.setCount(count);
            adapter.notifyDataSetChanged();
        }
    }

}
